package logic

import (
	"api/client"
	"api/request"
	"github.com/gin-gonic/gin"
	"net/http"
	user "user_srv/proto"
)

func Send(c *gin.Context) {
	var data request.SendRequest
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	res, err := client.SendSms(c, &user.SendSmsRequest{
		Tel: data.Tel,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	if !res.Success {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusInternalServerError,
			"msg":  "发送失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"data": nil,
		"msg":  "ok",
	})
}
